package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.common.utils.Tools;
import com.ld.hpfs.pojo.ExtPager;
import com.ld.hpfs.service.LeftService;
import com.ld.hpfs.web.interseptor.WebConstants;

/**
 * 主页面
 * 
 * @author bole
 * @date 2012-08-20 04:30:00
 */

@Controller
public class HomeController {
	
	@Autowired
	private LeftService leftService;
	
	/**
	 * 主页
	 */
	@RequestMapping("/")
	public String home(HttpSession session,HttpServletRequest request) {

		String lang = request.getLocale().getLanguage();
		if(lang.equals("en")){
			session.setAttribute(WebConstants.CURRENT_LANG, "en");
		} else {
			session.setAttribute(WebConstants.CURRENT_LANG, "zh_CN");
		}
		if (null != session.getAttribute(WebConstants.CURRENT_USER)) {
	        return "main";
	    }
	    
		// 转到登录页面
		return "index";
	}
	
	   @RequestMapping("/timeout")
	    public String timeout(HttpSession session) {
	        return "timeout";
	    }
	
	@RequestMapping("/report")
	public String reportviewer() {
		// 转到登录页面
		return "reportviewer";
	}

	/**
	 * 主页面
	 */
	@RequestMapping("/main")
	public String main(HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer) {
		String company_id = (String)request.getParameter("company_id") == null ? null : (String)request.getParameter("company_id");
		if(!Tools.isEmpty(company_id)){
			session.setAttribute(WebConstants.CURRENT_COMPANY, Integer.parseInt(company_id));
		}
		return "main";
	}

	/**
	 * 切换语言
	 */
	@RequestMapping("/changeLang")
	public String changeLang(HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer) {
		String lang = (String)request.getParameter("lang") == null ? null : (String)request.getParameter("lang");
		session.setAttribute(WebConstants.CURRENT_LANG, lang);
		return "main";
	}
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	private String formatResults(ArrayList<HashMap<String,Object>> result)
    {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        if (result != null)
        {
            for (int i = 0; i < result.size(); i++)
            {
            	HashMap<String,Object> account = result.get(i);
                String code = account.get("code").toString();
                String title = account.get("title").toString();

                sb.append("{'code':'").append(code).append("',").append("'title':'").append(title).append("'").append("}");

                if (i < result.size() - 1)
                {
                    sb.append(",");
                }
            }
        }
        sb.append("]");
        try
        {
            return sb.toString();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return "";
    }
	
}
